package com.whfc.wxmp.api.fse.service.impl;

import com.whfc.common.enums.BindFlag;
import com.whfc.common.enums.NetState;
import com.whfc.fse.dto.FseCraneDTO;
import com.whfc.fse.dto.FseCrawlerDTO;
import com.whfc.fse.dto.FseDeviceInfoDTO;
import com.whfc.fse.service.FseCraneService;
import com.whfc.fse.service.FseCrawlerService;
import com.whfc.wxmp.api.fse.service.WxFseService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021/11/4 16:33
 */
@Service
public class WxFseServiceImpl implements WxFseService {

    @DubboReference(interfaceClass = FseCraneService.class, version = "1.0.0")
    private FseCraneService fseCraneService;

    @DubboReference(interfaceClass = FseCrawlerService.class, version = "1.0.0")
    private FseCrawlerService fseCrawlerService;

    @Override
    public FseDeviceInfoDTO getFseDeviceInfo(Integer deptId) {
        FseDeviceInfoDTO fseDeviceInfoDTO = new FseDeviceInfoDTO();
        List<FseCraneDTO> craneList = fseCraneService.list(deptId, BindFlag.BIND.getValue()).getList();
        // 获取塔机数据
        int craneTotalNum = craneList.size();
        fseDeviceInfoDTO.setCraneTotalNum(craneTotalNum);
        int craneOnlineNum = 0;
        for (FseCraneDTO fseCraneDTO : craneList) {
            if (NetState.ONLINE.getValue().equals(fseCraneDTO.getNetState())) {
                craneOnlineNum += 1;
            }
        }
        fseDeviceInfoDTO.setCraneOnlineNum(craneOnlineNum);
        fseDeviceInfoDTO.setCraneOfflineNum(craneTotalNum - craneOnlineNum);

        //获取履带吊数据
        List<FseCrawlerDTO> crawlerList = fseCrawlerService.list(deptId, BindFlag.BIND.getValue(), null).getList();
        int crawlerTotalNum = crawlerList.size();
        fseDeviceInfoDTO.setCrawlerTotalNum(crawlerTotalNum);
        int crawlerOnlineNum = 0;
        for (FseCrawlerDTO fseCrawlerDTO : crawlerList) {
            if (NetState.ONLINE.getValue().equals(fseCrawlerDTO.getNetState())) {
                crawlerOnlineNum += 1;
            }
        }
        fseDeviceInfoDTO.setCrawlerOnlineNum(crawlerOnlineNum);
        fseDeviceInfoDTO.setCrawlerOfflineNum(crawlerTotalNum - crawlerOnlineNum);
        return fseDeviceInfoDTO;
    }
}
